#!/bin/bash

# This script generates a .deb package for the common files of OurGrid(ourgrid.org) modules
# It was based on rules provided by debian in http://www.debian.org/doc/maint-guide/

COMMON_FOLDER=./

#Defining some variables
PACKAGE_NAME=Common
TEMP_FOLDER=`mktemp -d -t Ourgrid-XXXXXX`
PACKAGE_TEMP_FOLDER=$TEMP_FOLDER/$PACKAGE_NAME
DEBIAN_FOLDER=$PACKAGE_TEMP_FOLDER/DEBIAN
BIN_FOLDER=$PACKAGE_TEMP_FOLDER/usr/bin
LIB_FOLDER=$PACKAGE_TEMP_FOLDER/usr/lib/ourgrid
CONF_FOLDER=$PACKAGE_TEMP_FOLDER/etc/ourgrid
LOG_FOLDER=$PACKAGE_TEMP_FOLDER/var/log/ourgrid
VERSION=1.0
CONTROL_FILE=""
CHANGELOG_FILE=""

# Functions

#Prints the help
usage()
{
	echo "Usage: ./common_package_generator.sh [OPTION]
Creates the Common package.

Mandatory arguments to long options are mandatory for short options too.
  -cf, --control     tells the control file location to the script
  -cl, --changelog   tells the changelog file location to the script
  -v,  --version     tells the the version of the package
  -h,  --help        display this help and exit

Exit status:
 0  if OK,
 1  for any problem"
}

#This part generates the directory's structure
generate_debian_structure()
{
	echo "Generating debian structure in '$PACKAGE_TEMP_FOLDER'"
	rm -rf $PACKAGE_TEMP_FOLDER
	mkdir -p $DEBIAN_FOLDER
	mkdir -p $BIN_FOLDER
	mkdir -p $LIB_FOLDER
	mkdir -p $CONF_FOLDER
	mkdir -p $LOG_FOLDER
}

#Copy peer files to package respective folders
copy_files()
{
	echo "Copying files from '$COMMON_FOLDER' to respective package directories"
	cp $COMMON_FOLDER/testjvm $BIN_FOLDER
	cp $COMMON_FOLDER/lib/* $LIB_FOLDER
	cp $COMMON_FOLDER/log4j.cfg.xml $CONF_FOLDER
	cp $COMMON_FOLDER/COPYING $CONF_FOLDER/COPYING
	cp $COMMON_FOLDER/COPYING $DEBIAN_FOLDER/copyright
}

#Create the control file
generate_control_file()
{
	
	if [ ! -z $CONTROL_FILE ]
	then
		echo "Copying the control file from '$CONTROL_FILE' to respective package directory"
		cp $CONTROL_FILE $DEBIAN_FOLDER/control
	else
		echo "Creating the control file in respective package directory"
		echo "Package: OurGrid-common
Version: $VERSION
Section: unknown
Priority: optional
Architecture: all
Depends: sun-java6-jre
Maintainer: OurGrid.org
Homepage: ourgrid.org
Description: This package installs the Ourgrid's common files." > $DEBIAN_FOLDER/control
	fi
}

#create the changelog file
generate_changelog_file()
{
	if [ ! -z $CHANGELOG_FILE ]
	then
		echo "Copying the changelog file from '$CHANGELOG_FILE' to respective package directory"
		cp $CHANGELOG_FILE $DEBIAN_FOLDER/changelog
	else
		echo "Creating the changelog file in respective package directory"
		echo "Current release $VERSION" > $DEBIAN_FOLDER/changelog
	fi
}

#create the rules file
generate_rules_file()
{
	echo "" > $DEBIAN_FOLDER/rules
	chmod +x $DEBIAN_FOLDER/rules
}

#Create extra scripts
generate_extra_scripts()
{
	#create the postinst file
	echo "Creating post install script"
	echo '#!/bin/bash
echo "Creating ourgrid user."
/usr/sbin/useradd ourgrid' > $DEBIAN_FOLDER/postinst
	chmod +x $DEBIAN_FOLDER/postinst
}

#MAIN

# Loop until all parameters are used up
while [ "$1" != "" ]; do
     case $1 in
        -v | --version )        shift
                                VERSION=$1
                                ;;
        -cf | --control )       shift
                                CONTROL_FILE=$1
                                ;;
        -cl | --changelog )     shift
                                CHANGELOG_FILE=$1
                                ;;
        -h | --help )           usage
                                exit 0
                                ;;
    esac
    # Shift all the parameters down by one
    shift
done

echo "Generating debian file with common files of OurGrid(ourgrid.org). The source files are in $COMMON_FOLDER."
generate_debian_structure
copy_files
generate_control_file
generate_changelog_file
#generate_rules_file
generate_extra_scripts

#Generate the package
echo "Generating the package into `pwd`"
dpkg --build $PACKAGE_TEMP_FOLDER .

#Remove the temp files
echo "Removing Temp files"
rm -rf $TEMP_FOLDER

exit 0

